<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );

define('PAGE_MAX', 10 );

if( !in_array( "A", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;

if( $_SERVER['REQUEST_METHOD'] == 'POST'){
	
	$forms = $_POST;
	$phase = 'complete';

	
	$db = new Class_DB;
	
	if( isset( $forms['chk'] ) ){
		foreach( $forms['chk'] as $id => $v ){

			// 現在ステータス
			// 1:下書き
			if( $forms['current_state'][$id] == '1'){
				if( isset( $forms['approve'][$id] ) && $forms['approve'][$id] == '2' ){
					$sql = sprintf("UPDATE D_SAMPLE SET status = '2', u_date = CURRENT_TIMESTAMP WHERE id = %u", $id );
					$db->query( $sql );
				}
			}
			// 2:承認依頼中
			if( $forms['current_state'][$id] == '2'){
				if( isset( $forms['approve'][$id] ) && $forms['approve'][$id] != '' ){
					$sql = sprintf("UPDATE D_SAMPLE SET status = '%s', u_date = CURRENT_TIMESTAMP WHERE id = %u", $forms['approve'][$id], $id );
					$db->query( $sql );
				}
			}
			// 3:承認済み
			if( $forms['current_state'][$id] == '3'){
				$sql = sprintf( "UPDATE D_SAMPLE SET settle_date = '%s', follow_result = '%s', follow_date1 = '%s', follow_text1 = '%s', follow_date2 = '%s', follow_text2 = '%s'",
					$forms['settle_date'][$id],
					$forms['follow_result'][$id],
					$forms['follow_date1'][$id],
					$db->quote( $forms['follow_text1'][$id] ),
					$forms['follow_date2'][$id],
					$db->quote( $forms['follow_text2'][$id] )
				);
				if( isset( $forms['approve'][$id] ) && $forms['approve'][$id] == '9' ){
					$sql .= ", status = '9'";
				}
				$sql .= sprintf(", u_date = CURRENT_TIMESTAMP WHERE id = %u", $id );
				$db->query( $sql );
			}
			// 4:非承認
			if( $forms['current_state'][$id] == '4'){
				// 更新なし
			}
			// 9:作業完了
			if( $forms['current_state'][$id] == '9'){
				$sql = sprintf( "UPDATE D_SAMPLE SET settle_date = '%s', follow_result = '%s', follow_date1 = '%s', follow_text1 = '%s', follow_date2 = '%s', follow_text2 = '%s'",
					$forms['settle_date'][$id],
					$forms['follow_result'][$id],
					$forms['follow_date1'][$id],
					$db->quote( $forms['follow_text1'][$id] ),
					$forms['follow_date2'][$id],
					$db->quote( $forms['follow_text2'][$id] )
				);
				if( isset( $forms['approve'][$id] ) && $forms['approve'][$id] == '9' ){
					$sql .= ", status = '9'";
				}
				$sql .= sprintf(", u_date = CURRENT_TIMESTAMP WHERE id = %u", $id );
				$db->query( $sql );
			}

		}
	}

	
	
}else{


	$forms = $_GET;
	$phase = 'input';

	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$page['current'] = $_GET['page'];
		$st = ($page['current'] -1) * PAGE_MAX;
	}else{
		$page['current'] = 1;
		$st = 0;
	}



	$db = new Class_DB;

	// タスク件数
	$task[0] = 0; $task[1] = 0; $task[2] = 0; $task[3] = 0; $task[4] = 0; $task[9] = 0;
	$sql = "SELECT status, count(status) AS cnt FROM D_SAMPLE GROUP BY status";
	$db->query( $sql );
	while( $row = $db->fetch() ){
		$task[ $row['status']] = $row['cnt'];
	}
	// 承認済み（確定納期内）
	$sql = sprintf( "SELECT count(*) AS cnt FROM D_SAMPLE WHERE status = '3' AND (settle_date = '' OR settle_date  >= '%s')", date("Y/m/d") );
	$db->query( $sql );
	$row = $db->fetch();
	$task[3] = $row['cnt'];
	// 承認済み（確定納期を過ぎてる）
	$sql = sprintf( "SELECT count(*) AS cnt FROM D_SAMPLE WHERE status = '3' AND (settle_date <> '' AND settle_date  < '%s')", date("Y/m/d") );
	$db->query( $sql );
	$row = $db->fetch();
	$task[5] = $row['cnt'];


	$sql		= "SELECT * FROM D_SAMPLE";
	$cnt_sql	= "SELECT COUNT(*) AS cnt FROM D_SAMPLE";
	$query		= "";

	/****  ID ****/
	$where0 = "";
	if( isset($forms['id']) && $forms['id'] != '' ){
		$where0 = sprintf( "id = %u AND ", $forms['id'] );
		$query .= "&id=" . $forms['id'];
	}


	/****  キーワード（製品名・納品先・依頼者名・納品先TEL ****/
	$where1 = "";
	if( isset($forms['keyword']) && $forms['keyword'] != '' ){
		$where1 = "(tel LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR company LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR request_name LIKE '%" . $db->quote( $forms['keyword'] ) . "%' OR search_text LIKE '%" . $db->quote( $forms['keyword'] ) . "%') AND ";
		$query .= "&keyword=" . $forms['keyword'];
	}

	/**** 作業状況 ****/
	$where2 = "";
	if( isset($forms['status']) && $forms['status'] ){
		
		// 承認済み かつ 確定の浮きを過ぎているもの
		if( $forms['status'] == 'past' ){
			$where2 = sprintf("status = '3' AND settle_date <> '' AND settle_date  < '%s' AND ", date("Y/m/d") );
		}elseif( $forms['status'] == '3' ){
			$where2 = sprintf("status = '3' AND (settle_date = '' OR settle_date  >= '%s') AND ", date("Y/m/d") );
		}else{
			$where2 = sprintf("status = '%s' AND ", $forms['status'] );
		}
		$query .= "&status=" . $forms['status'];
		
	}

	/****  フォロー記入 ****/
	$where3 = ""; $where4 = "";
	if( isset( $forms['follow'] ) ){

		if( in_array( 1, $forms['follow'] ) || in_array( 2, $forms['follow'] ) ){
			if( in_array( 1, $forms['follow'] ) ){
				$where3 = "(follow_text1 <> '' OR follow_text2 <> '') AND ";
				$query .= "&follow[]=1";
			}
			if( in_array( 2, $forms['follow'] ) ){
				$where4 = "(follow_text1 = '' AND follow_text2 = '') AND ";
				$query .= "&follow[]=2";
			}
		}
	}

	/**** 登録日 ****/
	$where5 = ""; $where6 = "";
	if( isset($forms['date1']) && $forms['date1'] != '' ){
		$where5 = sprintf("r_date >= '%s 00:00:00' AND ", $forms['date1'] );
		$query .= "&date1=" . $forms['date1'];
	}
	if( isset($forms['date2']) && $forms['date2'] != '' ){
		$where6 = sprintf("r_date <= '%s 23:59:59' AND ", $forms['date2'] );
		$query .= "&date2=" . $forms['date2'];
	}

	/**** 更新日 ****/
	$where7 = ""; $where8 = "";
	if( isset($forms['date3']) && $forms['date3'] != '' ){
		$where7 = sprintf("u_date >= '%s 00:00:00' AND ", $forms['date3'] );
		$query .= "&date3=" . $forms['date3'];
	}
	if( isset($forms['date4']) && $forms['date4'] != '' ){
		$where8 = sprintf("u_date <= '%s 23:59:59' AND ", $forms['date4'] );
		$query .= "&date4=" . $forms['date4'];
	}

	if( $where0 . $where1 . $where2 . $where3 . $where4 . $where5 . $where6 . $where7 . $where8 != "" ){
		$sql .= " WHERE ";
		$sql .= $where0 . $where1 . $where2 . $where3 . $where4 . $where5 . $where6 . $where7 . $where8;
		$sql = substr( $sql, 0, strlen($sql) -5 );

		$cnt_sql .= " WHERE ";
		$cnt_sql .= $where0 . $where1 . $where2 . $where3 . $where4 . $where5 . $where6 . $where7 . $where8;
		$cnt_sql = substr( $cnt_sql, 0, strlen($cnt_sql) -5 );
	}

	$sql .=  sprintf(" ORDER BY r_date DESC LIMIT %u, %u", $st, PAGE_MAX );


	// 件数取得・ページング
	$db->query( $cnt_sql );
	$row = $db->fetch();
	$page['cnt'] = $row['cnt'];
	$page['end'] = floor( $page['cnt'] / PAGE_MAX );
	if( $page['cnt'] % PAGE_MAX ) $page['end']++;
	$paging = array();
	if( $page['end'] >= 15 ){
		$pagecnt = 0;
		for( $i = $page['current'] -7; $i <= $page['end']; $i++ ){
			if( $i > 0 ){
				$paging[] = $i;
				$pagecnt++;
			}
			if( $pagecnt >= 15 ) break;
		}

	}else{
		// 全15件以下の場合は全件表示
		for( $i = 1; $i <= $page['end']; $i++ ){
			$paging[] = $i;
		}
	}

	$page['paging'] = $paging;
	$page['item'] = PAGE_MAX;
	$page['query'] = $query;

	
	//exit;

	// データ取得
	$db->query( $sql );
	$data = array();
	$ids = array();
	while( $row = $db->fetch() ){
		$ids[] = $row['id'];
		$row['disp_id'] = 'A' . sprintf('%05d', $row['id']);
		
		// ステータス
		// 1:下書き  2:承認依頼中  3:承認済み  4:非承認  9:作業完了
		if( $row['status'] == '1' ){
			$opt = "<label><input type=\"checkbox\" name=\"approve[" . $row['id'] . "]\" value=\"2\" />承認依頼</label>";
			$row['opt'] = $opt;
		}elseif( $row['status'] == '2' ){
			$opt  = "<select name=\"approve[" . $row['id'] . "]\"><option value=\"\"> </option>";
			$opt .= "<option value=\"3\">承認</option><option value=\"4\">非承認</option>";
			$opt .= "</select>";
			$row['opt'] = $opt;
		}elseif( $row['status'] == '3' ){
			$opt = "<input type=\"checkbox\" name=\"approve[" . $row['id'] . "]\" value=\"9\" />作業完了";
			$row['opt'] = $opt;
		}

		// フォロー
		// 確定納期
		if( $row['status'] == '3' || $row['status'] == '9' ){
			$follow = "<select name=\"follow_result[" . $row['id'] . "]\">";
			$follow .= "<option value=\"0\"></option>";
			$follow .= "<option value=\"1\"";
			if( $row['follow_result'] == '1' ) $follow .= " selected";
			$follow .= ">採用</option>";
			$follow .= "<option value=\"2\"";
			if( $row['follow_result'] == '2' ) $follow .= " selected";
			$follow .= ">不採用</option>";
			$follow .= "</select><br />";
			$follow .= "フォロー１：記入日 <input type=\"text\" name=\"follow_date1[" . $row['id'] . "]\" value=\"" . $row['follow_date1']. "\" class=\"text_date\" /><br />
<input type=\"text\" name=\"follow_text1[" . $row['id'] . "]\" id=\"text_information\" class=\"text\" value=\"" . $row['follow_text1'] . "\" /><br />";
			$follow .= "フォロー２：記入日 <input type=\"text\" name=\"follow_date2[" . $row['id'] . "]\" value=\"" . $row['follow_date2']. "\" class=\"text_date\" /><br />
<input type=\"text\" name=\"follow_text2[" . $row['id'] . "]\" id=\"text_information\" class=\"text\" value=\"" . $row['follow_text2'] . "\" />";
			$row['follow'] = $follow;
			$row['rowspan'] = 3;
			
			$row['settle_date'] = "<input type=\"text\" name=\"settle_date[" . $row['id'] . "]\" value=\"" . $row['settle_date'] . "\" class=\"text_date\"/>";
			
		}else{
			$row['rowspan'] = 2;
		}
		
		$data[] = $row;
	}

	$detail = array();
	if( count( $ids ) ){
		
		$sql = sprintf( "SELECT id, category, pro_name FROM D_SAMPLE_DETAIL WHERE id IN (%s)", implode(",", $ids ));
		$db->query( $sql );
		while( $row = $db->fetch() ){
			$detail[ $row['id'] ][] = $row;
		}
	}
	//print_r( $detail );


	$smarty->assign('arr_follow', array( 1 => 'あり', 2 => 'なし' ) );
	$smarty->assign('arr_status', $arr_status);
	$smarty->assign('category', $category);
	$smarty->assign('data', $data);
	$smarty->assign('detail', $detail);
	$smarty->assign('page', $page);
	if( isset( $forms ) ) $smarty->assign('forms', $forms);
	if( isset( $task ) ) $smarty->assign('task', $task);
}



$smarty->assign('phase', $phase);
$smarty->display('web-admin/sample_index.html');

?>